package com.fastplatform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.http.ResponseEntity;
import com.fastplatform.entity.User;
import com.fastplatform.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.fastplatform.dto.UserIdRequest;
import com.fastplatform.dto.AssignUserRolesRequest;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
@SaCheckLogin
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/list")
    public ResponseEntity<List<User>> getUserList(
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String userType,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            List<User> users = userService.getUserList(username, phone, status, userType, page, size);
            return ResponseEntity.ok(users);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    @PostMapping("/create")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        try {
            User createdUser = userService.createUser(user);
            return ResponseEntity.ok(createdUser);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    @PostMapping("/update")
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        try {
            User updatedUser = userService.updateUser(user);
            return ResponseEntity.ok(updatedUser);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    @PostMapping("/delete")
    public ResponseEntity<String> deleteUser(@RequestBody UserIdRequest request) {
        try {
            userService.deleteUser(request.getId());
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    @GetMapping("/roles")
    public ResponseEntity<List<Long>> getUserRoles(@RequestParam Long id) {
        try {
            List<Long> roleIds = userService.getUserRoles(id);
            return ResponseEntity.ok(roleIds);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }
    }
    
    @PostMapping("/roles")
    public ResponseEntity<String> assignUserRoles(@RequestBody AssignUserRolesRequest request) {
        try {
            userService.assignUserRoles(request.getId(), request.getRoleIds());
            return ResponseEntity.ok("分配成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}